package com.zmovie.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmovie.framework.domain.MovieToType;
import com.zmovie.framework.domain.MovieType;
import com.zmovie.framework.service.MovieToTypeService;
import com.zmovie.framework.mapper.MovieToTypeMapper;
import com.zmovie.framework.service.MovieTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【movie_to_type】的数据库操作Service实现
* @createDate 2023-11-30 12:27:26
*/
@Service
public class MovieToTypeServiceImpl extends ServiceImpl<MovieToTypeMapper, MovieToType>
    implements MovieToTypeService{
    @Autowired
    private MovieTypeService movieTypeService;
    @Override
    public List<MovieToType> listByMovieId(Integer movieId) {
        LambdaQueryWrapper<MovieToType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MovieToType::getMovieId, movieId);
        wrapper.orderByAsc(MovieToType::getMovieId);
        return list(wrapper);
    }

    @Override
    public List<MovieType> listTypeByMovieId(Integer movieId) {
        LambdaQueryWrapper<MovieToType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MovieToType::getMovieId, movieId);
        wrapper.orderByAsc(MovieToType::getMovieId);
        List<MovieToType> movieToTypes = list(wrapper);
        List<Integer> typeIds = movieToTypes.stream().map(MovieToType::getTypeId).collect(Collectors.toList());
        if(typeIds.size() == 0){
            return new ArrayList<>();
        }
        return movieTypeService.listByIds(typeIds);
    }

    @Override
    public List<MovieType> listRelatedMovieTypeByMovieId(Integer movieId) {
        //查已关联电影类型
        List<MovieToType> movieToTypeList = listByMovieId(movieId);
        //已关联类型id
        List<Integer> typeIds = movieToTypeList.stream().map(MovieToType::getTypeId).collect(Collectors.toList());
        if(typeIds.size() == 0){
            return new ArrayList<>();
        }
        return movieTypeService.listByIds(typeIds);
    }

    @Override
    public List<MovieType> listNotRelatedMovieTypeByMovieId(Integer movieId) {
        //查已关联电影类型
        List<MovieToType> movieToTypeList = listByMovieId(movieId);
        //已关联类型id
        List<Integer> typeIds = movieToTypeList.stream().map(MovieToType::getTypeId).collect(Collectors.toList());
        if(typeIds.size() == 0){
            //所有
            return movieTypeService.list();
        }
        return movieTypeService.listNotIn(typeIds);
    }
}




